import { Component, OnInit } from '@angular/core';
import { Event, Router } from '@angular/router';

import { BgService } from '../../../DM/background.service';
import { AccountService, User } from '../../../DM/account.service';

import { MenuLS, MenuItem } from '../../utils/menu';
@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.less']
})
export class MenuComponent implements OnInit {

  menu: MenuLS[] = [];

  selectedIndex: number;

  currentrouter: string;

  constructor(
    private bgService: BgService,
    private router: Router,
    private accountService: AccountService
  ) { }

  async ngOnInit() {
    this.menu = this.bgService.getMenuData();

    let currentuser: User = await this.accountService.getCurrentUserInfo();
    this.menu = this.menu.filter(item => {
      if (item.auth.indexOf(currentuser.Role.RoleId) != -1) return item;
    });
    this.disRouter();
  }

  disRouter(): void {
    this.menu.forEach(item => {
      item.child.forEach(child => {
        if (child.path.indexOf(this.router.url) == 0) {
          setTimeout(() => { this.emitEvent(child, item); }, 50);
        }
      });
    });
  }

  emitEvent(ite: MenuItem, menu: MenuLS) {
    this.bgService.emitMn.emit({ ite, menu });
  }
}
